library(foreign)
install.packages("ggplot2")
library(ggplot2)
install.packages("readstata13")
library(readstata13)

library(haven)

library(dplyr)
library(tidyr)
library(purrr)
install.packages("tidyverse")
library(tidyverse)

install.packages("extrafont")
library(extrafont)
font_import()
fonts()
loadfonts(device = "pdf")

lawyerdata <- read.dta13("HKCFA_u.dta")

##all cases cite
lcitedata <- lawyerdata[,c("year", "forei_nouk", "uk", "hk", "inter")]

df_long <- lcitedata %>% pivot_longer(cols = c(forei_nouk, uk, hk, inter), names_to = "Variable", values_to = "Value")

my_colors <- c("maroon", "navy", "blue", "forestgreen")

yearly_averages <- df_long %>%
  group_by(year, Variable) %>%
  summarize(Value = mean(Value, na.rm = TRUE))

p <- ggplot(yearly_averages, aes(x = year, y = Value, color = Variable)) +
  geom_point(size = 1) + xlab("") + ylab("Citation") + theme(plot.title = element_text(family = "Georgia", size = 14), axis.title = element_text(family = "Georgia", size = 12), axis.text = element_text(family = "Georgia", size = 10), legend.title = element_text(family = "Georgia", size = 12), legend.text = element_text(family = "Georgia", size = 10), panel.border = element_blank(),panel.background = element_blank(),axis.line = element_line(color = "black")) + scale_color_manual(values = my_colors, name = "Variable", labels = c("Foreign",  "HK", "International", "UK")) + scale_x_continuous(limits = c(1995, 2020), breaks = seq(1995, 2020, by = 5)) + scale_y_continuous(limits = c(0, 10), breaks = seq(0, 10, by = 5)) + labs(colour = "") 
# Add a loess smooth line with a ribbon for the confidence interval for each group
p <- p + geom_smooth(method = "loess", se = TRUE, level = 0.90, size = 0.5)

print(p)

ggsave("all.pdf", plot = p, width = 5, height = 4)

##public law case cite
lcitedata <- lawyerdata[lawyerdata$gov == 1,c("year", "forei_nouk", "uk", "hk", "inter")]

df_long <- lcitedata %>% pivot_longer(cols = c(forei_nouk, uk, hk, inter), names_to = "Variable", values_to = "Value")

my_colors <- c("maroon", "navy", "blue", "forestgreen")

yearly_averages <- df_long %>%
  group_by(year, Variable) %>%
  summarize(Value = mean(Value, na.rm = TRUE))

p <- ggplot(yearly_averages, aes(x = year, y = Value, color = Variable)) +
  geom_point(size = 1) + xlab("") + ylab("Citation") + theme(plot.title = element_text(family = "Georgia", size = 14), axis.title = element_text(family = "Georgia", size = 12), axis.text = element_text(family = "Georgia", size = 10), legend.title = element_text(family = "Georgia", size = 12), legend.text = element_text(family = "Georgia", size = 10), panel.border = element_blank(),panel.background = element_blank(),axis.line = element_line(color = "black")) + scale_color_manual(values = my_colors, name = "Variable", labels = c("Foreign",  "HK", "International", "UK")) + scale_x_continuous(limits = c(1995, 2020), breaks = seq(1995, 2020, by = 5)) + scale_y_continuous(limits = c(0, 10), breaks = seq(0, 10, by = 5)) + labs(colour = "") 
# Add a loess smooth line with a ribbon for the confidence interval for each group
p <- p + geom_smooth(method = "loess", se = TRUE, level = 0.90, size = 0.5)

print(p)

ggsave("public.pdf", plot = p, width = 5, height = 4)

##private law case cite
lcitedata <- lawyerdata[lawyerdata$gov == 0,c("year", "forei_nouk", "uk", "hk", "inter")]

df_long <- lcitedata %>% pivot_longer(cols = c(forei_nouk, uk, hk, inter), names_to = "Variable", values_to = "Value")

my_colors <- c("maroon", "navy", "blue", "forestgreen")

yearly_averages <- df_long %>%
  group_by(year, Variable) %>%
  summarize(Value = mean(Value, na.rm = TRUE))

p <- ggplot(yearly_averages, aes(x = year, y = Value, color = Variable)) +
  geom_point(size = 1) + xlab("") + ylab("Citation") + theme(plot.title = element_text(family = "Georgia", size = 14), axis.title = element_text(family = "Georgia", size = 12), axis.text = element_text(family = "Georgia", size = 10), legend.title = element_text(family = "Georgia", size = 12), legend.text = element_text(family = "Georgia", size = 10), panel.border = element_blank(),panel.background = element_blank(),axis.line = element_line(color = "black")) + scale_color_manual(values = my_colors, name = "Variable", labels = c("Foreign",  "HK", "International", "UK")) + scale_x_continuous(limits = c(1995, 2020), breaks = seq(1995, 2020, by = 5)) + scale_y_continuous(limits = c(0, 10), breaks = seq(0, 10, by = 5)) + labs(colour = "") 
# Add a loess smooth line with a ribbon for the confidence interval for each group
p <- p + geom_smooth(method = "loess", se = TRUE, level = 0.90, size = 0.5)

print(p)

ggsave("private.pdf", plot = p, width = 5, height = 4)




